﻿
using Microsoft.Office.Interop.Excel;
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;

namespace HualongExcel.Utilities
{
    internal static class ExcelHelper
    {
        /// <summary>
        /// 创建工作表
        /// </summary>
        /// <param name="sheetName"></param>
        /// <returns></returns>
        public static Worksheet CreateWorksheet(string sheetName = "")
        {
            //string path
            //打开实验数据
            //string str = @"E:\C#\Example200\LearnExcel\LearnExcel\data.xlsx";
            //Workbook wb = Common.CurrentExcelApp.Workbooks.Add(true);
            Worksheet ws = Common.CurrentExcelApp.Worksheets.Add();
            //Worksheet copy=null;
            //ws.Copy(copy);

            if (!string.IsNullOrEmpty(sheetName))
                ws.Name = sheetName;
            //....
            //ws.SaveAs(path);
            //关闭数据表
            //wb.Close();
            return ws;
        }

        public static Application OpenWorkbook(string path, bool replaceCurrentWindow = false)
        {
            Microsoft.Office.Interop.Excel.Application NewApp = new Microsoft.Office.Interop.Excel.Application();
            NewApp.Visible = true;
            //NewApp.Caption = "生成的表格";

            Workbook workbook = null;
            if (replaceCurrentWindow)
                workbook = NewApp.Workbooks.Open(path, UpdateLinks: 0);
            else
                workbook = NewApp.Workbooks.Add(path);

            return NewApp;
        }



        /// <summary>
        ///  获取单元格内容
        /// </summary>
        /// <param name="address"></param>
        /// <returns></returns>
        public static string GetCellContent(string address)
        {
            return Convert.ToString(Common.CurrentExcelApp.Range[address].Value);
        }

        /// <summary>
        /// 将excel中字符列转换为数字
        /// </summary>
        /// <param name="columnName">字母列名称</param>
        /// <returns></returns>
        public static int ToIndex(this string columnName)
        {
            if (!Regex.IsMatch(columnName.ToUpper(), @"[A-Z]+")) { throw new Exception("invalid parameter"); }


            int index = 0;
            char[] chars = columnName.ToUpper().ToCharArray();
            for (int i = 0; i < chars.Length; i++)
            {
                index += ((int)chars[i] - (int)'A' + 1) * (int)Math.Pow(26, chars.Length - i - 1);
            }
            return index - 1;
        }
        /// <summary>
        /// 将数字转换为excel中字母列
        /// </summary>
        /// <param name="index">数字</param>
        /// <returns></returns>
        public static string ToName(this int index)
        {
            if (index < 0) { throw new Exception("invalid parameter"); }

            List<string> chars = new List<string>();
            do
            {
                if (chars.Count > 0) index--;
                chars.Insert(0, ((char)(index % 26 + (int)'A')).ToString());
                index = (int)((index - index % 26) / 26);
            } while (index > 0);

            return String.Join(string.Empty, chars.ToArray());
        }
    }

}
